﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using IssueCommon;
using ValidateCommon;
using Common;
using BusinessLogic.BusinessRules;
namespace RichECMS.ServerPages.Template
{
    public partial class UpdateTemplate : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Tid"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Tid"].ToString().Trim()))
                    {
                        InitDataBind(Convert.ToInt32(Request["Tid"]));
                        LitParentPath.Text = State.LoginUser.TemplatePath.Replace(Server.MapPath(Request.Path).Replace("ServerPages\\Template\\UpdateTemplate.aspx", "").ToLower(), "");
                    }
                    else
                    {
                        Alert("参数错误");
                    }
                }
                else
                {
                    Alert("参数错误");
                }
            }
        }

        /// <summary>
        /// 执行上传
        /// </summary>
        private string UploadExec()
        {
            if (FileUploadTemplate.FileName.Length != 0)
            {
                string strfilepath = State.LoginUser.TemplatePath + "\\" + FileUploadTemplate.FileName;
                FileUploadTemplate.SaveAs(strfilepath);
                return FileUploadTemplate.FileName;
            }
            else if (txtTemplatePath.Value.Trim().Length != 0)
            {
                return txtTemplatePath.Value;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 页面输出
        /// </summary>
        private void InitDataBind(int intTemplateId)
        {
            try
            {
                //绑定类型
                ListItem listitem = new ListItem("--请选择模版类型--", "-1");
                DropDownListType.Items.Add(listitem);
                foreach (int i in Enum.GetValues(typeof(Utility.TemplateType)))
                {
                    listitem = new ListItem(Utility.GetTemplateType(i), i.ToString());
                    DropDownListType.Items.Add(listitem);
                }

                ////绑定网站
                //listitem = new ListItem("--请选择站点--", "-1");
                //dropWebSiteList.Items.Add(listitem);
                //using (WebSiteFactory objFactory = new WebSiteFactory())
                //{
                //    BusinessLogic.BusinessRules.WebSite objWebSite = objFactory.Creator();
                //    if (objWebSite != null)
                //    {
                //        for (int i = 0; i < objWebSite.Count; i++)
                //        {
                //            listitem = new ListItem(objWebSite.DataItem.chvName, objWebSite.DataItem.intWebSiteID.ToString());
                //            dropWebSiteList.Items.Add(listitem);
                //            objWebSite.SelectRow(i + 1);
                //        }
                //    }
                //}
                using (TemplateFactory objFactory = new TemplateFactory())
                {
                    BusinessLogic.BusinessRules.Template objTemplate = objFactory.Creator(intTemplateId);
                    if (objTemplate != null)
                    {
                        txtDateInTime.Text = objTemplate.DataItem.dtmInTime.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                        this.txtTemplatePath.Value = objTemplate.DataItem.chvPath;
                        this.txtTitle.Value = objTemplate.DataItem.chvName;
                        foreach (ListItem item in DropDownListType.Items)
                        {
                            if (item.Value == objTemplate.DataItem.intType.ToString())
                            {
                                DropDownListType.SelectedIndex = DropDownListType.Items.IndexOf(item);
                                break;
                            }
                        }
                        //foreach (ListItem item in dropWebSiteList.Items)
                        //{
                        //    if (item.Value == objTemplate.DataItem.intWSID.ToString())
                        //    {
                        //        dropWebSiteList.SelectedIndex = dropWebSiteList.Items.IndexOf(item);
                        //        break;
                        //    }
                        //}
                        foreach (ListItem item in RadioButtonListEnableUse.Items)
                        {
                            if (item.Value == objTemplate.DataItem.intState.ToString())
                            {
                                RadioButtonListEnableUse.SelectedIndex = RadioButtonListEnableUse.Items.IndexOf(item);
                                break;
                            }
                        }
                    }
                    else
                    {
                        Alert("该文章不存在");
                    }
                }

                using (WebSiteFactory obgWebFactory = new WebSiteFactory())
                {
                    BusinessLogic.BusinessRules.WebSite objWebsite = obgWebFactory.Creator(State.intWSID);
                    if (objWebsite != null)
                    {
                        LitWebTitle.Text = objWebsite.DataItem.chvTitle;
                    }
                    else
                    {
                        Alert("该网站数据有误，请联系网站管理员");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(txtDateInTime.Text)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }
            //if (dropWebSiteList.SelectedValue.Trim() == "-1")
            //{
            //    Alert("请选择站点");
            //    bolIsValidate = false;
            //}
            if (DropDownListType.SelectedValue.Trim() == "-1")
            {
                Alert("请选择模版类型");
                bolIsValidate = false;
            }

            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }
            using (TemplateFactory objFactory = new TemplateFactory())
            {
                BusinessLogic.BusinessRules.Template objTemplate = objFactory.Creator(Convert.ToInt32(Request["Tid"]));
                objTemplate.DataItem.chvName = this.txtTitle.Value;
                objTemplate.DataItem.intType = Convert.ToInt32(DropDownListType.SelectedValue.Trim());
                objTemplate.DataItem.intWSID = State.intWSID;
                objTemplate.DataItem.dtmInTime = Convert.ToDateTime(txtDateInTime.Text);
                objTemplate.DataItem.intState = Convert.ToInt32(RadioButtonListEnableUse.SelectedValue.Trim());
                objTemplate.DataItem.chvPath = UploadExec();
                //if (FileUploadTemplate.FileName.Trim().Length != 0)
                //{
                //    objTemplate.DataItem.chvPath = UploadExec();
                //}
                //else
                //{
                //    objTemplate.DataItem.chvPath = this.txtTemplatePath.Value;
                //}
                objTemplate.Update();
            }
            AlertAndRedirect("修改模版成功！即将跳转，请稍候", "TemplateManage.aspx", false);
        }
    }
}